Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | export const dynamic = "force-dynamic"; import { NextRequest, NextResponse } from 'next/server'; import { } from "next-auth"; import { prisma } from "@/lib/prisma"; import { z } from "zod"; import { withAdmin, withErrorHandling, successResponse, ApiError, ApiSuccessResponse, ApiErrorResponse } from "@/lib/api"; import { RouteContext } from "@/lib/api/middleware"; interface RouteParams { params: Promise<{ id: string }>; } const UpdateCampaignSchema = z.object({ name: z.string().min(1).optional(), referrerReward: z .object({ type: z.enum(["points", "credit", "discount"]), value: z.number(), discountType: z.enum(["percentage", "fixed"]).optional() }) .optional(), refereeReward: z .object({ type: z.enum(["points", "credit", "discount"]), value: z.number(), discountType: z.enum(["percentage", "fixed"]).optional() }) .optional(), minPurchase: z.number().nullable().optional(), isActive: z.boolean().optional() }); /** * GET /api/admin/referrals/campaigns/[id] * Get campaign details */ async function handleGet(_request: NextRequest, context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const campaignId = parseInt(id); const campaign = await prisma.referralProgram.findUnique({ where: { id: campaignId } }); if (!campaign) { throw ApiError.notFound("Campaign"); } // Get referral stats const [totalReferrals, completedReferrals, pendingReferrals] = await Promise.all([ prisma.referral.count({ where: { programId: campaignId } }), prisma.referral.count({ where: { programId: campaignId, status: "rewarded" } }), prisma.referral.count({ where: { programId: campaignId, status: "pending" } }), ]); return successResponse({ ...campaign, stats: { totalReferrals, completedReferrals, pendingReferrals } }); } /** * PATCH /api/admin/referrals/campaigns/[id] * Update campaign */ async function handlePatch(request: NextRequest, context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const campaignId = parseInt(id); const body = await request.json(); const result = UpdateCampaignSchema.safeParse(body); if (!result.success) { throw ApiError.validation("Invalid campaign data", result.error.issues); } const validatedData = result.data; const campaign = await prisma.referralProgram.findUnique({ where: { id: campaignId } }); if (!campaign) { throw ApiError.notFound("Campaign"); } const updatedCampaign = await prisma.referralProgram.update({ where: { id: campaignId }, data: { name: validatedData.name, referrerReward: validatedData.referrerReward, refereeReward: validatedData.refereeReward, minPurchase: validatedData.minPurchase, isActive: validatedData.isActive } }); return successResponse(updatedCampaign); } /** * DELETE /api/admin/referrals/campaigns/[id] * Delete campaign */ async function handleDelete(_request: NextRequest, context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { id } = await (context as RouteParams).params; const campaignId = parseInt(id); const campaign = await prisma.referralProgram.findUnique({ where: { id: campaignId } }); if (!campaign) { throw ApiError.notFound("Campaign"); } // Check for existing referrals const referralCount = await prisma.referral.count({ where: { programId: campaignId } }); if (referralCount > 0) { throw ApiError.badRequest( `Cannot delete campaign with ${referralCount} referrals. Deactivate instead.` ); } await prisma.referralProgram.delete({ where: { id: campaignId } }); return successResponse({ message: "Campaign deleted successfully" }); } export const GET = withErrorHandling(withAdmin(handleGet)); export const PATCH = withErrorHandling(withAdmin(handlePatch)); export const DELETE = withErrorHandling(withAdmin(handleDelete)); |